package com.edward.lock;

import android.os.Bundle;
import android.app.Activity;
import android.app.admin.DevicePolicyManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;

public class DeviceAdminActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.activity_device_admin);
		 
	}
	
	@Override
	protected void onResume(){
	myLockScreen();
	super.onResume();
	}
	
	public void myLockScreen(){		
		
        DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);                                 
      
        ComponentName componentName = new ComponentName(this, AdminReceiver.class);        		      
      
        boolean isAdminActive = mDPM.isAdminActive(componentName);	
		
        
		if(isAdminActive){            
            
		 mDPM.lockNow();    	
		 
		finish();
        } else {
			//set active by user
            Intent intent = new Intent();           
            intent.setAction(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);            
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            startActivity(intent);
        }	
        
	}
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_device_admin, menu);
		return true;
	}

}
